package com.hna.eking.AirlineServer.Platform.FileUpload;

import java.util.List;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpSession;  
//import com.smax.ppd.util.FileUploadListener;  
import org.apache.commons.fileupload.FileItem;  
import org.apache.commons.fileupload.FileUpload;  
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;  
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;  
import org.springframework.web.multipart.MultipartException;  
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

//@Component("multipartResolver")
public class CommonsMultipartResolverExt extends CommonsMultipartResolver {
	
//	  @Autowired
//	  private FileUploadListener fileUploadListener; 
	  
//	  public void setFileUploadProgressListener( 
//			  FileUploadListener fileUploadListener) { 
//	    this.fileUploadListener = fileUploadListener; 
//	  } 

	@Override  
    protected MultipartParsingResult parseRequest(HttpServletRequest request)  
            throws MultipartException {  
        final HttpSession hs = request.getSession();  
        FileUploadListener listener = new FileUploadListener(hs); //利用构造方法传递参数  
        String encoding = determineEncoding(request);  
        FileUpload fileUpload = prepareFileUpload(encoding);  
        fileUpload.setProgressListener(listener);  
        try {  
            List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);  
            return parseFileItems(fileItems, encoding);  
        }  
        catch (FileUploadBase.SizeLimitExceededException ex) {  
            throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
        } 
        catch (FileUploadBase.FileSizeLimitExceededException ex) {  
            throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
        }  
        catch (FileUploadException ex) {  
            throw new MultipartException("Could not parse multipart servlet request", ex);  
        }  
    } 
	

}
